#include <string>
#include <map>
#include <numeric>
#include <algorithm>
#include <vector>

#include <iostream>
#include <sstream>

template<typename T>
std::ostream& operator<<(std::ostream & os, std::vector<T> const & vec) {
    using std::to_string;
    std::ostringstream oss;
    for (auto const & val : vec) {
        oss << val << " ";
    }
    auto s = oss.str();
    s.pop_back();
    os << s;
    return os;
}

int main() {
    using Output = std::vector<int>;

    // 今天才知道原来 vector 可以直接比较字典序

    std::map<Output, int> count;

    int N, M;
    std::cin >> N >> M;

    for (int i = 0; i < N; ++i) {
        Output out;
        out.reserve(M);
        for (int j = 0; j < M; ++j) {
            int val;
            std::cin >> val;
            out.push_back(val);
        }
        auto it = count.find(out);
        if (it != count.end()) {
            count[out] += 1;
        } else {
            count[out] = 1;
        }
    }
    using Pair = std::pair<int, Output>;
    std::vector<Pair> results;
    for (auto pair : count) {
        results.push_back({pair.second, pair.first});
    }
    std::sort(results.begin(), results.end(), [](Pair const & a, Pair const & b){
        return a.first > b.first || (
            a.first == b.first && a.second < b.second
        );
    });

    std::cout << results.size() << "\n";

    for (auto pair : results) {
        std::cout << pair.first << " " << pair.second << "\n";
    }
}
